package com.letere.controller;

import com.letere.bean.Blog;
import com.letere.bean.Result;
import com.letere.service.BlogService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author letere
 * @create 2021-02-05 11:22
 */
@RestController
public class BlogController {

    @Autowired
    private BlogService blogService;

    @ApiOperation("获取博客文章")
    @GetMapping("/blog")
    public Result getBlogById(@ApiParam(value = "博客文章id", required = true) Integer id) {
        Blog blog = blogService.getBlogById(id);
        return Result.success(blog);
    }

    @ApiOperation("获取博客文章（模糊搜索）")
    @ApiImplicitParam(name = "title", value = "标题", required = true)
    @GetMapping("/blog/search")
    public Result getBlogByTitle(String title) {
        List<Blog> blogs = blogService.getBlogByTitle(title);
        return Result.success(blogs);
    }

    @ApiOperation("获取博客文章集合（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码", required = true),
            @ApiImplicitParam(name = "limit", value = "页面数据大小", required = true)
    })
    @GetMapping("/blogs")
    public Result getBlogsByPage(Integer currentPage, Integer limit) {
        List<Blog> blogs = blogService.getBlogsByPage(currentPage, limit);
        return Result.success(blogs);
    }

    @ApiOperation("获取博客文章集合（分类）")
    @ApiImplicitParam(name = "category", value = "分类名", required = true)
    @GetMapping("/blog/category")
    public Result getBlogsByCategories(String category) {
        List<Blog> blogs = blogService.getBlogByCategories(category);
        return Result.success(blogs);
    }

    @ApiOperation("获取分类名集合")
    @GetMapping("/blog/categories")
    public Result getCategories() {
        List<String> categories = blogService.getCategories();
        return Result.success(categories);
    }

    @ApiOperation("获取博客文章数据大小")
    @GetMapping("/blogSize")
    public Result getBlogSize() {
        Integer size = blogService.getBlogSize();
        return Result.success(size);
    }

    @ApiOperation("新增博客文章")
    @PostMapping("/blog/add")
    public void addBlog(@RequestBody Blog blog) {
        blog.setCreated(new Date());
        blogService.addBlog(blog);
    }

    @ApiOperation("更新博客文章")
    @PostMapping("/blog/update")
    public void updateBlog(@RequestBody Blog blog) {
        blog.setUpdated(new Date());
        blogService.updateBlog(blog);
    }

    @ApiOperation("删除博客文章")
    @ApiImplicitParam(name = "id", value = "博客文章id", required = true)
    @PostMapping("/blog/delete")
    public void deleteBlog(Integer id) {
        blogService.deleteBlogById(id);
    }
}
